<template>
  <div>
    <div class="btn-group-wrapper bottom">
      <el-button-group>
        <el-button type="primary" icon="plus" @click="addHandler">新闻添加</el-button>
        <el-button type="primary" icon="delete" @click="delHandler">删除</el-button>
      </el-button-group>
      <el-input
        style="width: 250px"
        placeholder="请输入关键字"
        icon="search"
        v-model="searchText"
        :on-icon-click="searchHandler">
      </el-input>
    </div>
    <el-table ref="newsTable" :data="getNews" height="500" border :stripe="true" :highlight-current-row="true"
              @selection-change="handleSelectionChange"
              style="width: 100%">
      <el-table-column
        type="selection"
        width="40">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        :show-overflow-tooltip="true"
        width="275">
      </el-table-column>
      <el-table-column
        prop="publishDate"
        :formatter="formatPTime"
        label="发布时间">
      </el-table-column>
      <el-table-column
        prop="modifyDate"
        :formatter="formatMTime"
        label="修改时间">
      </el-table-column>
      <el-table-column
        prop="location"
        label="发布位置"
        width="80">
      </el-table-column>
      <el-table-column
        prop="source"
        label="来源媒体"
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="80">
      </el-table-column>
      <el-table-column
        prop="publisher"
        label="发布人"
        width="80">
      </el-table-column>
      <el-table-column inline-template label="操作" width="130" fixed="right">
      <span>
        <el-button type="info" size="small" @click="deleteItemHandler($index,row)">删除</el-button>
        <el-button type="info" size="small" @click="editItemHandler($index, row)">修改</el-button>
      </span>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysNewsCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getSysNewsTotalCount">
      </el-pagination>
    </div>
  </div>
</template>
<script>

  import {mapActions, mapGetters} from 'vuex'
  import moduleStore from './bll/store'
  import store from '../../store'
  import dateUtil from '../../common/dateUtil';
  (!store.state.sysNewsBroadcast) && store.registerModule('sysNewsBroadcast', moduleStore)
  export default {
    data() {
      return {
        pageSize: 10,
        searchText: '',
        multipleSelection: []
      }
    },
    computed: {
      ...mapGetters(['getNews', 'getSysNewsTotalCount', 'getSysNewsCurrentPage', 'getNewsSearchText'])
    },
    mounted() {
      //回到index时editingNews为空
      this.setEditingNews({});
      this.setPageModel('ADD');
      this.initPage();
    },
    methods: {
      ...mapActions(['querySysNews', 'searchSysNews', 'deleteNews', 'setEditingNews', 'setPageModel', 'setSearchText']),
      initPage() {
        let searchText = this.getNewsSearchText;
        if (searchText === '') {
          this.querySysNews();
        } else {
          this.searchText = searchText;
          this.searchSysNews({searchText});
        }
      },
      formatPTime(row, column) {
        if (row.publishDate) {
          return dateUtil.getDateTime(new Date(row.publishDate));
        } else {
          return '';
        }
      },
      formatMTime(row, column) {
        if (row.modifyDate) {
          return dateUtil.getDateTime(new Date(row.modifyDate));
        } else {
          return '';
        }
      },
      addHandler() {
        this.setPageModel('ADD');
        this.$router.push('/newsBroadcast/add');
      },
      deleteConfirm(fun, params) {
        this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          fun(params).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.initPage()
          });
        });
      },
      delHandler() {
        if (this.multipleSelection.length === 0) {
          this.$message.warning('请选择要删除的条目');
        } else if (this.multipleSelection.length > 1) {
          this.$message.warning('目前不能多项操作');
        } else {
          this.deleteConfirm(this.deleteNews, this.multipleSelection[0]);
        }
      },
      editItemHandler(index, row) {
        //设置要修改的内容
        this.setPageModel('UPDATE');
        this.setEditingNews(row);
        this.$router.push('/newsBroadcast/add');
      },
      deleteItemHandler(index, row) {
        this.deleteConfirm(this.deleteNews, row);
      },
      searchHandler() {
        let searchText = this.searchText;
        this.setSearchText(searchText);
        if (searchText === '') {
          this.querySysNews();
        } else {
          this.searchSysNews({searchText});
        }
      },
      handleSelectionChange(val) {
        //val: [{信息}]
        this.multipleSelection = val;
      },
      handleSizeChange (val) {
        let searchText = this.getNewsSearchText;
        this.pageSize = val;
        if (searchText === '') {
          this.querySysNews({index: 1, size: val});
        } else {
          this.searchSysNews({searchText: searchText, index: 1, size: val});
        }
      },
      handleCurrentChange (val) {
        let searchText = this.getNewsSearchText;
        if (searchText === '') {
          this.querySysNews({index: val, size: this.pageSize});
        } else {
          this.searchSysNews({searchText: searchText, index: val, size: this.pageSize});
        }
      }
    }
  }
</script>
<style>
</style>

